<template>
  <div class="col-md-4">
    <form class="form-horizontal">
      <div class="form-group">
        <label>用户名</label>
        <input
          type="text"
          class="form-control"
          placeholder="用户名"
          v-model="comment.username"
        />
      </div>
      <div class="form-group">
        <label>评论内容</label>
        <textarea
          class="form-control"
          rows="6"
          placeholder="评论内容"
          v-model="comment.content"
        ></textarea>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="button" class="btn btn-default pull-right" @click="add">
            提交
          </button>
        </div>
      </div>
    </form>
  </div>
</template>
<script>
export default {
  name: "AddComment",
  data() {
    return {
      // 评论数据
      comment: {
        username: "",
        content: "",
      },
    };
  },
  methods: {
    // 添加评论
    add() {
      // 数据不能为空
      let { username, content } = this.comment;
      if (!username) {
        alert("用户名不能为空");
        return;
      }
      if (!content) {
        alert("评论内容不能为空");
        return;
      }

      this.$bus.$emit("addComment", this.comment);

      this.comment.username = "";
      this.comment.content = "";
    },
  },
};
</script>
<style></style>